package com.roc.steelend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.buy.JuanBanBuyOrderVo;
import com.roc.steelend.controller.vo.buy.OtherProductsBuyOrderVo;
import com.roc.steelend.controller.vo.buy.PingBanBuyOrderVo;
import com.roc.steelend.controller.vo.common.JuanBanSaleBuyOrderDetailVo;
import com.roc.steelend.controller.vo.common.OtherProductsSaleBuyOrderDetailVo;
import com.roc.steelend.controller.vo.common.PingBanSaleBuyOrderDetailVo;
import com.roc.steelend.controller.vo.common.BuyByRecoding;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.mapper.BuyDetailMapper;
import com.roc.steelend.mapper.BuyOrderMapper;
import com.roc.steelend.mapper.InventoryMapper;
import com.roc.steelend.mapper.SuppliersMapper;
import com.roc.steelend.pojo.BuyDetail;
import com.roc.steelend.pojo.BuyOrder;
import com.roc.steelend.pojo.Inventory;
import com.roc.steelend.pojo.Suppliers;
import com.roc.steelend.service.BuyOrderService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class BuyOrderServiceImpl  implements BuyOrderService {
    @Autowired
    private SuppliersMapper suppliersMapper;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private BuyDetailMapper buyDetailMapper;
//  平板库存查询wrapper
    private static QueryWrapper<Inventory> PingBanWrapperUtil(BuyDetail buyDetail, Integer warehouseId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id",warehouseId);
        wrapper.eq("small_type_id",buyDetail.getSmallTypeId());
        wrapper.eq("thickness",buyDetail.getThickness());
        wrapper.eq("long1",buyDetail.getLong1());
        wrapper.eq("wide",buyDetail.getWide());
        wrapper.eq("high",buyDetail.getHigh());
        return wrapper;
    }
//  卷板库存查询wrapper
    private static QueryWrapper<Inventory> JuanBanWrapperUtil(BuyDetail buyDetail, Integer warehouseId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id",warehouseId);
        wrapper.eq("small_type_id",buyDetail.getSmallTypeId());
        wrapper.eq("thickness",buyDetail.getThickness());
        if (warehouseId!=3){
            wrapper.eq("long1",buyDetail.getLong1());
            wrapper.eq("wide",buyDetail.getWide());
        }
        return wrapper;
    }
//    其他产品库存查询wrapper
    private static QueryWrapper<Inventory> OtherProductsWrapperUtil(BuyDetail buyDetail, Integer warehouseId) {
    QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
    wrapper.eq("warehouse_id",warehouseId);
    wrapper.eq("small_type_id",buyDetail.getSmallTypeId());
    wrapper.eq("specification",buyDetail.getSpecification());
    return wrapper;
}


    @Override
    public ReturnMsg addBuyPingBanOrderService(PingBanBuyOrderVo pingBanBuyOrderVo,Integer employeeId) {

//        判断是否要注册新的供应商
        Boolean tempBySupplierInsert=false;
         if (pingBanBuyOrderVo.getIsNewSupplier()){
             Suppliers supplier = pingBanBuyOrderVo.getOtherMsg().getSupplier();
             supplier.setSignupTime(new Timestamp(new Date().getTime()));
             supplier.setDebt(new BigDecimal("0"));
             supplier.setInvoice(new BigDecimal("0"));
             int insert = suppliersMapper.insert(supplier);
             if (insert==1){
                 tempBySupplierInsert=true;
             }
         }

         Integer supplierId=null;
         if (pingBanBuyOrderVo.getIsNewSupplier()){
             QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
             wrapper.orderByDesc("supplier_id");
             wrapper.last("LIMIT 1");
             List<Suppliers> suppliers = suppliersMapper.selectList(wrapper);
             supplierId=suppliers.get(0).getSupplierId();
         }else {
             tempBySupplierInsert=true;
             supplierId=pingBanBuyOrderVo.getOtherMsg().getSupplier().getSupplierId();
         }
//
        ReturnMsg msg = new ReturnMsg();
        if (tempBySupplierInsert){
//              1、进行供应商扣款     计算总共要多少钱(是要包括运费的)
            BigDecimal freight = pingBanBuyOrderVo.getOtherMsg().getFreight();
            BigDecimal count = pingBanBuyOrderVo.getOtherMsg().getCount();
            BigDecimal suppliersNewDebt= freight.add(count);
            suppliersMapper.updateSupplierDebt(supplierId,suppliersNewDebt);
//            2、操作采购订单大表   生成订单号,员工id,供应商id,库房id,运费,总价(不含运费),操作订单时间，入库时间.备注
            BigInteger buyLsh = LshUtil.getBuyNo1TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date storageTime = pingBanBuyOrderVo.getOtherMsg().getOrderTime();
            Integer warehouseId = pingBanBuyOrderVo.getOtherMsg().getWarehouseId();
            String remark = pingBanBuyOrderVo.getOtherMsg().getRemark();
            BuyOrder buyOrder = new BuyOrder();
            buyOrder.setBuyOrderId(buyLsh);
            buyOrder.setEmployeeId(employeeId);
            buyOrder.setSumMoney(suppliersNewDebt);
            buyOrder.setInvoiceMoney(suppliersNewDebt);
            buyOrder.setInvoiceIsSettled(0);
            buyOrder.setSupplierId(supplierId);
            buyOrder.setWarehouseId(warehouseId);
            buyOrder.setFreight(freight);
            buyOrder.setRemark(remark);
            buyOrder.setStorageDate(storageTime);
            buyOrder.setOperateTime(operateTime);
            buyOrder.setPaymentIsSettled(0);
            buyOrder.setOrderTotalMoney(count);
            buyOrder.setGiven(new BigDecimal("0"));
            buyOrder.setIsReconciliation(0);
            buyOrder.setIsWriteOff(0);
            buyOrder.setIsInvoice(0);
            buyOrder.setIsTiaoPiao(0);
            buyOrder.setTiaoPiaoId(null);
            buyOrder.setMoLingTou(0);
            buyOrderMapper.insert(buyOrder);
//            3、 操作采购订单细节表,需要当前大表的流水号
            List<PingBanSaleBuyOrderDetailVo> pingBanBuyOrderDetailVoList = pingBanBuyOrderVo.getBuyPingBanOrderDetailVo();
            ArrayList<BuyDetail> buyDetailArrayList = new ArrayList<>();
            for (int i = 0; i <pingBanBuyOrderDetailVoList.size() ; i++) {
                PingBanSaleBuyOrderDetailVo orderDetailVo = pingBanBuyOrderDetailVoList.get(i);
                BuyDetail buyDetail = new BuyDetail();
                buyDetail.setBuyOrderId(buyLsh);
                buyDetail.setSmallTypeId(orderDetailVo.getLeibie());
                buyDetail.setSpecification(orderDetailVo.getHoudu().toString());
                buyDetail.setThickness(orderDetailVo.getHoudu());
                buyDetail.setLong1(orderDetailVo.getLong1());
                buyDetail.setWide(orderDetailVo.getWide());
                buyDetail.setHigh(orderDetailVo.getHeight());
                buyDetail.setQuantity(orderDetailVo.getQuantity());
                buyDetail.setPrice(orderDetailVo.getPrice());
                buyDetail.setWeight(orderDetailVo.getWeight());
                buyDetail.setWay(Integer.valueOf(orderDetailVo.getWay()));
                buyDetailArrayList.add(buyDetail);
            }

            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                buyDetailMapper.insert(buyDetail);
            }
//          4、对库存表进行一系列更新
            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = PingBanWrapperUtil(buyDetail, 3);
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                    Inventory inventory = inventoryList.get(0);
                    Integer temp=inventory.getQuantity()+buyDetail.getQuantity() ;//-5+6
                    if (temp>0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                        QueryWrapper<Inventory> secondWrapper = PingBanWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                        List<Inventory> inventoryList1 = inventoryMapper.selectList(secondWrapper);
                        if (inventoryList1.isEmpty()){
                            inventory.setInventoryId(null);
                            inventory.setWarehouseId(buyOrder.getWarehouseId());
                            inventory.setQuantity(temp);
                            inventoryMapper.insert(inventory);
                        }else {
                            Inventory inventory1 = inventoryList1.get(0);
                            Integer oldQuantity = inventory1.getQuantity();
                            Integer newQuantity=oldQuantity+temp;
                            inventory1.setQuantity(newQuantity);
                            inventoryMapper.updateById(inventory1);
                        }
                    }else if (temp==0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                    }else {
                        inventory.setQuantity(temp);
                        inventoryMapper.updateById(inventory);
                    }
                }else {
                    QueryWrapper<Inventory> threeWrapper = PingBanWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(threeWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(buyDetail.getSmallTypeId());
                        inventory3.setWarehouseId(buyOrder.getWarehouseId());
                        inventory3.setSpecification(buyDetail.getSpecification());
                        inventory3.setThickness(buyDetail.getThickness());
                        inventory3.setLong1(buyDetail.getLong1());
                        inventory3.setWide(buyDetail.getWide());
                        inventory3.setHigh(buyDetail.getHigh());
                        inventory3.setQuantity(buyDetail.getQuantity());
                        inventory3.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory3);
                    }else{
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity+buyDetail.getQuantity();
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);

                    }
                }
            }
            msg.setMsg("添加平板采购订单成功");
            msg.setCode(1);
            return  msg;
        }else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }


    }

    @Override
    public ReturnMsg addBuyJuanBanOrderService(JuanBanBuyOrderVo juanBanBuyOrderVo, Integer employeeId) {
        Boolean tempBySupplierInsert = false;
        if (juanBanBuyOrderVo.getIsNewSupplier()) {
            Suppliers supplier = juanBanBuyOrderVo.getOtherMsg().getSupplier();
            supplier.setSignupTime(new Timestamp(new Date().getTime()));
            supplier.setDebt(new BigDecimal("0"));
            supplier.setInvoice(new BigDecimal("0"));
            int insert = suppliersMapper.insert(supplier);
            if (insert == 1) {
                tempBySupplierInsert = true;
            }
        }

        Integer supplierId = null;
        if (juanBanBuyOrderVo.getIsNewSupplier()) {
            QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("supplier_id");
            wrapper.last("LIMIT 1");
            List<Suppliers> suppliers = suppliersMapper.selectList(wrapper);
            supplierId = suppliers.get(0).getSupplierId();
        } else {
            tempBySupplierInsert = true;
            supplierId = juanBanBuyOrderVo.getOtherMsg().getSupplier().getSupplierId();
        }
//
        ReturnMsg msg = new ReturnMsg();
        if (tempBySupplierInsert) {
//              1、进行供应商扣款     计算总共要多少钱(是要包括运费的)
            BigDecimal freight = juanBanBuyOrderVo.getOtherMsg().getFreight();
            BigDecimal count = juanBanBuyOrderVo.getOtherMsg().getCount();
            BigDecimal suppliersNewDebt= freight.add(count);
            suppliersMapper.updateSupplierDebt(supplierId,suppliersNewDebt);
//            2、操作采购订单大表   生成订单号,员工id,供应商id,库房id,运费,总价(不含运费),操作订单时间，入库时间.备注
            BigInteger buyLsh = LshUtil.getBuyNo2TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date storageTime = juanBanBuyOrderVo.getOtherMsg().getOrderTime();
            Integer warehouseId = juanBanBuyOrderVo.getOtherMsg().getWarehouseId();
            String remark = juanBanBuyOrderVo.getOtherMsg().getRemark();
            BuyOrder buyOrder = new BuyOrder();
            buyOrder.setBuyOrderId(buyLsh);
            buyOrder.setSumMoney(suppliersNewDebt);
            buyOrder.setInvoiceMoney(suppliersNewDebt);
            buyOrder.setInvoiceIsSettled(0);
            buyOrder.setEmployeeId(employeeId);
            buyOrder.setSupplierId(supplierId);
            buyOrder.setWarehouseId(warehouseId);
            buyOrder.setFreight(freight);
            buyOrder.setRemark(remark);
            buyOrder.setStorageDate(storageTime);
            buyOrder.setOperateTime(operateTime);
            buyOrder.setPaymentIsSettled(0);
            buyOrder.setOrderTotalMoney(count);
            buyOrder.setGiven(new BigDecimal("0"));
            buyOrder.setIsReconciliation(0);
            buyOrder.setIsWriteOff(0);
            buyOrder.setIsInvoice(0);
            buyOrder.setIsTiaoPiao(0);
            buyOrder.setTiaoPiaoId(null);
            buyOrder.setMoLingTou(0);
            buyOrderMapper.insert(buyOrder);
//            3、 操作采购订单细节表,需要当前大表的流水号
            List<JuanBanSaleBuyOrderDetailVo> juanBanBuyOrderDetailVoList = juanBanBuyOrderVo.getBuyJuanBanOrderDetailVo();
            ArrayList<BuyDetail> buyDetailArrayList = new ArrayList<>();
            for (int i = 0; i <juanBanBuyOrderDetailVoList.size() ; i++) {
                JuanBanSaleBuyOrderDetailVo orderDetailVo = juanBanBuyOrderDetailVoList.get(i);
                BuyDetail buyDetail = new BuyDetail();
                buyDetail.setBuyOrderId(buyLsh);
                buyDetail.setSmallTypeId(orderDetailVo.getLeibie());
                buyDetail.setSpecification(orderDetailVo.getHoudu().toString());
                buyDetail.setThickness(orderDetailVo.getHoudu());
                buyDetail.setLong1(orderDetailVo.getLong1());
                buyDetail.setWide(orderDetailVo.getWide());
                buyDetail.setHigh(new BigDecimal("0"));
                buyDetail.setQuantity(1);
                buyDetail.setPrice(orderDetailVo.getPrice());
                buyDetail.setWeight(orderDetailVo.getWeight());
                buyDetail.setWay(1);
                buyDetailArrayList.add(buyDetail);
            }

            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                buyDetailMapper.insert(buyDetail);
            }

//          4、对库存表进行一系列更新
            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = JuanBanWrapperUtil(buyDetail, 3);
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                    Inventory inventory = inventoryList.get(0);
                    Integer temp=inventory.getQuantity()+buyDetail.getQuantity() ;//-5+6
                    if (temp>0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                        QueryWrapper<Inventory> secondWrapper = JuanBanWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                        List<Inventory> inventoryList1 = inventoryMapper.selectList(secondWrapper);
                        if (inventoryList1.isEmpty()){
                            inventory.setInventoryId(null);
                            inventory.setQuantity(temp);
                            inventory.setWarehouseId(buyOrder.getWarehouseId());
                            inventoryMapper.insert(inventory);
                        }else {
                            Inventory inventory1 = inventoryList1.get(0);
                            Integer oldQuantity = inventory1.getQuantity();
                            Integer newQuantity=oldQuantity+temp;
                            inventory1.setQuantity(newQuantity);
                            inventoryMapper.updateById(inventory1);
                        }
                    }else if (temp==0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                    }else {
                        inventory.setQuantity(temp);
                        inventoryMapper.updateById(inventory);
                    }
                }else {
                    QueryWrapper<Inventory> threeWrapper = JuanBanWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(threeWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(buyDetail.getSmallTypeId());
                        inventory3.setWarehouseId(buyOrder.getWarehouseId());
                        inventory3.setSpecification(buyDetail.getSpecification());
                        inventory3.setThickness(buyDetail.getThickness());
                        inventory3.setLong1(buyDetail.getLong1());
                        inventory3.setWide(buyDetail.getWide());
                        inventory3.setHigh(buyDetail.getHigh());
                        inventory3.setQuantity(buyDetail.getQuantity());
                        inventory3.setWeight(buyDetail.getWeight());
                        inventoryMapper.insert(inventory3);
                    }else{
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity+buyDetail.getQuantity();
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);

                    }
                }
            }
            msg.setMsg("添加卷板采购订单成功");
            msg.setCode(1);
            return  msg;
        } else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }

    }

    @Override
    public ReturnMsg addBuyOtherProductsOrderService(OtherProductsBuyOrderVo otherProductsBuyOrderVo, Integer employeeId) {
//        判断是否要注册新的供应商
        Boolean tempBySupplierInsert=false;
        if (otherProductsBuyOrderVo.getIsNewSupplier()){
            Suppliers supplier = otherProductsBuyOrderVo.getOtherMsg().getSupplier();
            supplier.setSignupTime(new Timestamp(new Date().getTime()));
            supplier.setDebt(new BigDecimal("0"));
            supplier.setInvoice(new BigDecimal("0"));
            int insert = suppliersMapper.insert(supplier);
            if (insert==1){
                tempBySupplierInsert=true;
            }
        }

        Integer supplierId=null;
        if (otherProductsBuyOrderVo.getIsNewSupplier()){
            QueryWrapper<Suppliers> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("supplier_id");
            wrapper.last("LIMIT 1");
            List<Suppliers> suppliers = suppliersMapper.selectList(wrapper);
            supplierId=suppliers.get(0).getSupplierId();
        }else {
            tempBySupplierInsert=true;
            supplierId=otherProductsBuyOrderVo.getOtherMsg().getSupplier().getSupplierId();
        }

        ReturnMsg msg = new ReturnMsg();

        if (tempBySupplierInsert){
//              1、进行供应商扣款     计算总共要多少钱(是要包括运费的)
            BigDecimal freight = otherProductsBuyOrderVo.getOtherMsg().getFreight();
            BigDecimal count = otherProductsBuyOrderVo.getOtherMsg().getCount();
            BigDecimal suppliersNewDebt= freight.add(count);
            suppliersMapper.updateSupplierDebt(supplierId,suppliersNewDebt);
//            2、操作采购订单大表   生成订单号,员工id,供应商id,库房id,运费,总价(不含运费),操作订单时间，入库时间.备注
            BigInteger buyLsh = LshUtil.getBuyNo3TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date storageTime = otherProductsBuyOrderVo.getOtherMsg().getOrderTime();
            Integer warehouseId = otherProductsBuyOrderVo.getOtherMsg().getWarehouseId();
            String remark = otherProductsBuyOrderVo.getOtherMsg().getRemark();
            BuyOrder buyOrder = new BuyOrder();
            buyOrder.setBuyOrderId(buyLsh);
            buyOrder.setEmployeeId(employeeId);
            buyOrder.setSumMoney(suppliersNewDebt);
            buyOrder.setInvoiceMoney(suppliersNewDebt);
            buyOrder.setInvoiceIsSettled(0);
            buyOrder.setSupplierId(supplierId);
            buyOrder.setWarehouseId(warehouseId);
            buyOrder.setFreight(freight);
            buyOrder.setRemark(remark);
            buyOrder.setStorageDate(storageTime);
            buyOrder.setOperateTime(operateTime);
            buyOrder.setPaymentIsSettled(0);
            buyOrder.setOrderTotalMoney(count);
            buyOrder.setGiven(new BigDecimal("0"));
            buyOrder.setIsReconciliation(0);
            buyOrder.setIsWriteOff(0);
            buyOrder.setIsInvoice(0);
            buyOrder.setIsTiaoPiao(0);
            buyOrder.setTiaoPiaoId(null);
            buyOrder.setMoLingTou(0);
            buyOrderMapper.insert(buyOrder);

//            3、 操作采购订单细节表,需要当前大表的流水号
            List<OtherProductsSaleBuyOrderDetailVo> otherProductsBuyOrderDetailVoList = otherProductsBuyOrderVo.getBuyOtherProductsOrderDetailVo();
            ArrayList<BuyDetail> buyDetailArrayList = new ArrayList<>();
            for (int i = 0; i <otherProductsBuyOrderDetailVoList.size() ; i++) {
                OtherProductsSaleBuyOrderDetailVo orderDetailVo = otherProductsBuyOrderDetailVoList.get(i);
                BuyDetail buyDetail = new BuyDetail();
                buyDetail.setBuyOrderId(buyLsh);
                buyDetail.setSmallTypeId(orderDetailVo.getLeibie());
                buyDetail.setSpecification(orderDetailVo.getSpecification());
                buyDetail.setThickness(new BigDecimal("0"));
                buyDetail.setLong1(new BigDecimal("0"));
                buyDetail.setWide(new BigDecimal("0"));
                buyDetail.setHigh(new BigDecimal("0"));
                buyDetail.setQuantity(orderDetailVo.getQuantity());
                buyDetail.setPrice(orderDetailVo.getPrice());
                if (orderDetailVo.getWay()){
                    buyDetail.setWeight(orderDetailVo.getWeight());
                    buyDetail.setWay(1);
                }else {
                    buyDetail.setWeight(new BigDecimal("0"));
                    buyDetail.setWay(3);
                }
                buyDetailArrayList.add(buyDetail);
            }

            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                buyDetailMapper.insert(buyDetail);
            }
//          4、对库存表进行一系列更新
            for (int i = 0; i <buyDetailArrayList.size() ; i++) {
                BuyDetail buyDetail = buyDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = OtherProductsWrapperUtil(buyDetail, 3);
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                    Inventory inventory = inventoryList.get(0);
                    Integer temp=inventory.getQuantity()+buyDetail.getQuantity() ;//-5+6
                    if (temp>0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                        QueryWrapper<Inventory> secondWrapper = JuanBanWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                        List<Inventory> inventoryList1 = inventoryMapper.selectList(secondWrapper);
                        if (inventoryList1.isEmpty()){
                            inventory.setInventoryId(null);
                            inventory.setQuantity(temp);
                            inventory.setWarehouseId(buyOrder.getWarehouseId());
                            inventoryMapper.insert(inventory);
                        }else {
                            Inventory inventory1 = inventoryList1.get(0);
                            Integer oldQuantity = inventory1.getQuantity();
                            Integer newQuantity=oldQuantity+temp;
                            inventory1.setQuantity(newQuantity);
                            inventoryMapper.updateById(inventory1);
                        }
                    }else if (temp==0){
                        inventoryMapper.deleteById(inventory.getInventoryId());
                    }else {
                        inventory.setQuantity(temp);
                        inventoryMapper.updateById(inventory);
                    }
                }else {
                    QueryWrapper<Inventory> threeWrapper = OtherProductsWrapperUtil(buyDetail, buyOrder.getWarehouseId());
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(threeWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(buyDetail.getSmallTypeId());
                        inventory3.setWarehouseId(buyOrder.getWarehouseId());
                        inventory3.setSpecification(buyDetail.getSpecification());
                        inventory3.setThickness(buyDetail.getThickness());
                        inventory3.setLong1(buyDetail.getLong1());
                        inventory3.setWide(buyDetail.getWide());
                        inventory3.setHigh(buyDetail.getHigh());
                        inventory3.setQuantity(buyDetail.getQuantity());
                        inventory3.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory3);
                    }else{
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity+buyDetail.getQuantity();
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);

                    }
                }
            }
            msg.setMsg("添加其他产品采购订单成功");
            msg.setCode(1);
            return  msg;
        }else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }
    }

    @Override
    public List<BuyOrder> buyFindRecoding(BuyByRecoding selectMsg) {
        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
         now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        Integer warehouseId = selectMsg.getWarehouseId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        String supplierName = selectMsg.getSupplierName();
        if (supplierName != null && !supplierName.equals("")){
            supplierName="%"+supplierName+"%";
        }
        Boolean showAllPayInvoice = selectMsg.getShowAllPayInvoice();
        Boolean showPay = selectMsg.getShowPay();
        Boolean showInvoice = selectMsg.getShowInvoice();
        List<java.sql.Date> timeZone = selectMsg.getTimeZone();
        java.sql.Date startDate =null;
        java.sql.Date endDate =null;
        if (timeZone!=null){
          startDate = timeZone.get(0);
          endDate = timeZone.get(1);
        }

        List<BuyOrder> buyOrderList = buyOrderMapper.findALlMsg(bigTypeId, smallTypeId, warehouseId, specification, supplierName, showAllPayInvoice, showPay, showInvoice, startDate, endDate);
        return  buyOrderList;
    }

    @Override
    public Integer buyFindRecodingCount(BuyByRecoding selectMsg) {
        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
        now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        Integer warehouseId = selectMsg.getWarehouseId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        String supplierName = selectMsg.getSupplierName();
        if (supplierName != null && !supplierName.equals("")){
            supplierName="%"+supplierName+"%";
        }
        Boolean showAllPayInvoice = selectMsg.getShowAllPayInvoice();
        Boolean showPay = selectMsg.getShowPay();
        Boolean showInvoice = selectMsg.getShowInvoice();
        List<java.sql.Date> timeZone = selectMsg.getTimeZone();
        java.sql.Date startDate =null;
        java.sql.Date endDate =null;
        if (timeZone!=null){
            startDate = timeZone.get(0);
            endDate = timeZone.get(1);
        }
        return buyOrderMapper.buyFindRecodingCount(bigTypeId,smallTypeId,warehouseId,specification,supplierName,showAllPayInvoice,showPay,showInvoice,startDate,endDate);
    }







}
